package com.auxgroup.smarthome.inner.service;

import com.auxgroup.bridge.app.inner.dto.AppFilterFaultDto;
import com.auxgroup.bridge.app.inner.vo.AppFilterFaultVo;
import com.auxgroup.smarthome.BeanUtils;
import com.auxgroup.smarthome.constant.cache.CachedConsant;
import com.auxgroup.smarthome.inner.entity.AppFilterFaultEntity;
import com.auxgroup.smarthome.inner.repo.AppFilterFaultRepo;
import com.auxgroup.smarthome.redis.config.ObjectRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;

/**
 * 滤网故障service
 * Created by lixiaoxiao on 17-8-14.
 */
@Service
public class AppFilterFaultService {

    @Autowired
    private AppFilterFaultRepo appFilterFaultRepo;

    @Autowired
    private ObjectRedis objectRedis;

    private final String pathKey = CachedConsant.DEVICE_FILTER_FAULT_PREFIX;

    /**
     * 确认滤网清洗
     * 调用此接口，则把标志位设为false,并且把开机时间小时置为0
     * @param appFilterFaultDto
     * @return
     */
    public boolean confirmFilterWashed(AppFilterFaultDto appFilterFaultDto,String uid) {
        String mac = appFilterFaultDto.getMac();
        String key = pathKey+mac;
        AppFilterFaultEntity appFilterFaultEntity = appFilterFaultRepo.findByMacAndWashTrue(mac);;
        if(appFilterFaultEntity == null || appFilterFaultEntity.getBootTime() < 500*60){
            return false;
        }
        appFilterFaultEntity.setBootTime(0);
        appFilterFaultEntity.setWash(false);
        appFilterFaultEntity.setConfirmBy(uid);
        appFilterFaultEntity.setConfirmTime(Instant.now().getEpochSecond());
        appFilterFaultRepo.save(appFilterFaultEntity);
        //清空缓存
        objectRedis.delete(key);
        return true;
    }

    /**
     * 根据mac查询数据
     * @param mac
     * @return
     */
    public AppFilterFaultVo getFilterFalut(String mac) {
        //String key = pathKey+mac;
        AppFilterFaultEntity appFilterFaultEntity = appFilterFaultRepo.findByMacAndWashTrue(mac);
        if(appFilterFaultEntity == null){
            return null;
        }
        /*if(objectRedis.get(key,AppFilterFaultEntity.class) == null){
            appFilterFaultEntity = appFilterFaultRepo.findByMacAndWashTrue(mac);
        }else{
            appFilterFaultEntity = objectRedis.get(key,AppFilterFaultEntity.class);
        }*/
        return BeanUtils.copyAttrs(new AppFilterFaultVo(),appFilterFaultEntity);
    }

    /**
     * 通过mac地址获取app滤网信息，business服务使用
     * @param mac
     * @return
     */
    public AppFilterFaultVo getFilterFalutData(String mac) {
        AppFilterFaultEntity appFilterFaultEntity = appFilterFaultRepo.findByMac(mac);
        if(null == appFilterFaultEntity) {
            return null;
        }
        return BeanUtils.copyAttrs(new AppFilterFaultVo(),appFilterFaultEntity);
    }

    /**
     * 计算设备滤网时间
     * 业务逻辑:
     * 每接收一条开机数据，操作缓存数据库，当收到关机数据时，持久化数据
     * @param mac
     * @param realOnOff
     * @param fromReport
     * @return
     */
    public boolean calculateTime(String mac,Boolean realOnOff,Boolean fromReport){
        //十分钟上报并且开机情况下
        mac = mac.toUpperCase();
        String key = pathKey+mac;
        if(fromReport == true && realOnOff == true){
            //如果缓存不存在,去数据库查找数据是否存在
            if(objectRedis.get(key,AppFilterFaultEntity.class) == null){
                AppFilterFaultEntity appFilterFaultEntity = appFilterFaultRepo.findByMacAndWashFalse(mac);
                //如果数据库存在数据,添加缓存
                if(appFilterFaultEntity != null){
                    appFilterFaultEntity.setBootTime(appFilterFaultEntity.getBootTime()+10);
                    objectRedis.add(key,1000L,appFilterFaultEntity);
                }else{
                    AppFilterFaultEntity appFilterFaultEntityInit = new AppFilterFaultEntity();
                    appFilterFaultEntityInit.setBootTime(10);
                    appFilterFaultEntityInit.setMac(mac);
                    appFilterFaultEntityInit.setWash(false);
                    objectRedis.add(key,1000L,appFilterFaultEntityInit);
                }
            }else{
                //如果缓存不为空,把缓存的时间加十分钟
                AppFilterFaultEntity appFilterFaultEntity = objectRedis.get(key,AppFilterFaultEntity.class);
                appFilterFaultEntity.setBootTime(appFilterFaultEntity.getBootTime()+10);
                objectRedis.add(key,1000L,appFilterFaultEntity);
            }
        }else if(fromReport == true && realOnOff == false){
            //关机状态下 清空缓存并且持久化数据
            if(objectRedis.get(key,AppFilterFaultEntity.class) != null){
                appFilterFaultRepo.save(objectRedis.get(key,AppFilterFaultEntity.class));
                objectRedis.delete(key);
            }
        }
        return true;
    }

}